﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace landlord2lib
{
    public class CardGroupPair : CardGroup
    {
        public CardGroupPair(Card[] cards)
        {
            m_cards = cards;
        }

        public override bool IsValid()
        {
            try
            {
                if (m_cards.Length != CardGroupPair.CardCount) return false;
                foreach (Card card in m_cards) card.Reset();

                Card cardReal = null;
                foreach (Card card in m_cards)
                {
                    if (!card.IsJoker)
                    {
                        cardReal = card;
                        break;
                    }
                }
                if (cardReal != null)
                {
                    foreach (Card card in m_cards)
                    {
                        if (card.IsJoker) card.ComparePoint = cardReal.ComparePoint;
                    }
                }
                return m_cards[0].ComparePoint == m_cards[1].ComparePoint;
            }
            catch
            {
                return false;
            }
        }

        public override int CompareTo(CardGroup other)
        {
            try
            {
                if (other is CardGroupPair)
                {
                    CardGroupPair o = (CardGroupPair)other;
                    return m_cards[0].CompareTo(o.m_cards[0]);
                }
                else return -1;
            }
            catch
            {
                return 0;
            }
        }

        public static int CardCount
        {
            get
            {
                return 2;
            }
        }

        public static HintCardCollection Hint(Card[] handCards, CardGroupPair lastGroup)
        {
            HintCardCollection result = new HintCardCollection();
            try
            {
                CardStack stack = new CardStack(handCards);
                CardPoint minPt = lastGroup.Cards[0].ComparePoint;
                for (int pt = (int)minPt + 1; pt <= (int)CardPoint.Card2; pt++)
                {
                    if (stack[pt].Count == 2)
                    {
                        List<Card> cards = new List<Card>();
                        for (int i = 0; i < 2; i++) cards.Add(stack[pt][i]);
                        result.AddHint(cards.ToArray());
                    }
                }
                for (int pt = (int)CardPoint.CardGJoker; pt <= (int)CardPoint.CardJoker; pt++)
                {
                    if (stack[pt].Count == 2) result.AddHint(new Card[] { stack[pt][0], stack[pt][1] });
                }
                return result;
            }
            catch
            {
                return result;
            }
        }
    }
}
